package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.stagecheck.entity.StageCheckVO;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoServerQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.stagecheck.rmitf.StageCheckQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StageCheckNoticeConstructor extends AbstractMessageNoticeConstructor {

    private final ProjectSimpleQuery projectSimpleQuery;

    private final StageCheckQuery stageCheckQuery;
    @Autowired
    private EnterpriseInfoServerQuery teamQuery;

    @Autowired
    public StageCheckNoticeConstructor(ProjectSimpleQuery projectSimpleQuery,
                                       StageCheckQuery stageCheckQuery) {
        this.projectSimpleQuery = projectSimpleQuery;
        this.stageCheckQuery = stageCheckQuery;
    }


    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if(noticeVO == null){
            return null;
        }
        StageCheckVO stageCheckVO = stageCheckQuery.queryOneByPk(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(stageCheckVO.getPkProject());

        String prefix = getApprovalPrefixMessage(stageCheckVO.getBillstatus());
        EnterpriseInfo enterpriseInfo = teamQuery.queryEnterpriseById(stageCheckVO.getPkConstructionunit());
        String name = stageCheckVO.getWorkqualityVO().getCheckname();
        if (enterpriseInfo != null) {
            name = enterpriseInfo.getName();
        }
        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" + name + "]的阶段验收，" + prefix;
        noticeVO.setMessageTitle(projectVO.getSname());
        noticeVO.setMessageReceiver(stageCheckVO.getCreator());
        noticeVO.setPkTenant(stageCheckVO.getPkTenant());
        noticeVO.setMessageContent(messageContent);
        noticeVO.setRelateType(stageCheckVO.getBillType());

        return new MessageNoticeVO[]{noticeVO};
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.STAGECHECK_BILLTYPE;
    }
}
